package TileGUI;

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import javax.swing.*;
import java.util.Random;


public class Board extends JPanel implements ActionListener {
    Dude dude;
    public Image img;
    Timer timer;
    public static final int GRAVITY = 1;
    private ArrayList<Enemy> enemies;
    private int B_WIDTH = super.getHeight();
    private int B_HEIGHT = super.getWidth();
    private boolean ingame;
    private Random rnd = new Random(System.currentTimeMillis());
    long prevT, currT;

    private int[][] pos = {
        {4380, 29}, {4500, 59}, {2380, 89},
        {1600, 109}, {1080, 139}, {1280, 239},
        {1490, 259}, {1460, 50}, {1490, 150},
        {1880, 209}, {1060, 45}, {1010, 70},
        {1830, 159}, {1090, 80}, {1030, 60},
        {1840, 59}, {990, 30}, {920, 200},
        {1600, 259}, {1260, 50}, {940, 90},
        {810, 220}, {860, 20}, {1440, 180},
        {1020, 128}, {890, 170}, {1300, 30}
     };


    public Board() {
            dude = new Dude();
            addKeyListener(new AL());
            setFocusable(true);
            setBackground(Color.BLACK);
            ImageIcon i = new ImageIcon("images/newbg.png");
            img = i.getImage();
            timer = new Timer(5, this);
            timer.start();
            ingame = true;
            prevT=0;

            initEnemies();
    }

    public void addNotify() {
        super.addNotify();
        B_WIDTH = getWidth();
        B_HEIGHT = getHeight();
    }

    public void initEnemies() {
        enemies = new ArrayList<Enemy>();

        /*
        for (int i=0; i<pos.length; i++ ) {
            enemies.add(new Enemy(pos[i][0], pos[i][1]));
        }*/

        for (int i=0; i < 40; i++) {
            enemies.add(new Enemy(rnd.nextInt(8000)+1000,rnd.nextInt(375)));
        }
    }

    public void actionPerformed(ActionEvent e) {

            if (enemies.size()==0) {
                ingame = false;
            }
            ArrayList ms = dude.getMissiles();

            for (int i = 0; i < ms.size(); i++) {
                Missile m = (Missile) ms.get(i);
                if (m.isVisible()) 
                    m.move();
                else ms.remove(i);
            }

            for (int i = 0; i < enemies.size(); i++) {
                Enemy a = (Enemy) enemies.get(i);
                if (a.isVisible())
                    a.move();
                else enemies.remove(i);
            }

            dude.move();
            checkCollisions();
            repaint();
    }

    public void paint(Graphics g) {
            super.paint(g);

            if (ingame) {
            Graphics2D g2d = (Graphics2D) g;
            
            if((dude.getX() - 590) % 2400 == 0){
            	
            	dude.nx = 0;
            	
            }
            if((dude.getX() - 1790) % 2400 == 0){
            	
            	dude.nx2 = 0;
            	
            }

            g2d.drawImage(img, 685-dude.nx2, 0, null);
            if(dude.getX() >= 590){
            	 g2d.drawImage(img, 685-dude.nx, 0, null);
            	
            }
            ArrayList ms = dude.getMissiles();

            for (int i = 0; i < ms.size(); i++ ) {
                Missile m = (Missile) ms.get(i);
                g2d.drawImage(m.getImage(), m.getX(), m.getY(), null);
            }

            for (int i = 0; i < enemies.size(); i++) {
                Enemy a = (Enemy)enemies.get(i);
                if (a.isVisible())
                    g2d.drawImage(a.getImage(), a.getX(), a.getY(), null);
            }

            g2d.drawString("enemies left: " + enemies.size(), 5, 15);
            g2d.drawImage(dude.getImage(), 75, dude.getY(), null);

        }else {
            String msg = "Game Over";
            Font small = new Font("Helvetica", Font.BOLD, 14);
            FontMetrics metr = this.getFontMetrics(small);

            g.setColor(Color.white);
            g.setFont(small);
            g.drawString(msg, (700 - metr.stringWidth(msg)) / 2,
                         265 / 2);
        }

            Toolkit.getDefaultToolkit().sync();
            g.dispose();
    }

    public void checkCollisions() {

        Rectangle r3 = dude.getBounds();

        for (int j = 0; j<enemies.size(); j++) {
            Enemy a = (Enemy) enemies.get(j);
            Rectangle r2 = a.getBounds();

            if (r3.intersects(r2)) {
                dude.setVisible(false);
                a.setVisible(false);
                ingame = false;
            }
        }

        ArrayList ms = dude.getMissiles();

        for (int i = 0; i < ms.size(); i++) {
            Missile m = (Missile) ms.get(i);

            Rectangle r1 = m.getBounds();

            for (int j = 0; j<enemies.size(); j++) {
                Enemy a = (Enemy) enemies.get(j);
                Rectangle r2 = a.getBounds();

                if (r1.intersects(r2)) {
                    m.setVisible(false);
                    a.setVisible(false);
                }
            }
        }
    }

    private class AL extends KeyAdapter {
            public void keyReleased(KeyEvent e) {
                    dude.keyReleased(e);
            }

            public void keyPressed(KeyEvent e) {
                    dude.keyPressed(e);
            }
    }
}